package test_achieve.web;

import constant.Constant;
import cucumber.api.PendingException;
import cucumber.api.java.en.Given;
import org.junit.Assert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;

import java.util.List;

/**
 * Created by chenkuo on 2017/7/25.
 */
public class MyOrder {
    OrderMain webmain = new OrderMain();
    IncreasePrice inc = new IncreasePrice();

    @Given("^w取消刚下的订单$")
    public void w取消刚下的订单() throws Throwable {
        List<WebElement> pageElement = Constant.driverWeb.findElements(By.linkText("取消订单"));
        pageElement.get(0).click();//点击第一个取消按钮
        String orderNumber = Constant.OrderNumbuter.get(Constant.OrderNumbuter.size()-1);
        Thread.sleep(1500);
        Constant.driverWeb.findElement(By.xpath("//*[@id=\"cancelorder_form"+orderNumber+"\"]/div/div[3]/button[1]")).click();//点击确定取消
        Thread.sleep(2000);
    }

    @Given("^w订单钱退回到用户余额$")
    public void w订单钱退回到用户余额() throws Throwable {
        inc.w进入账户余额();
        Thread.sleep(2000);
        WebElement balance = Constant.driverWeb.findElement(By.xpath("//*[@id=\"app\"]/div/div/div[2]/div[2]/div/div[1]/span[2]/span[1]"));//获取账户余额
        String UserBalance = balance.getText();//实际余额
        String UserBalance2 =Constant.userBalance;//用户之前余额=理论余额
        Assert.assertEquals(UserBalance, UserBalance2);

    }

    @Given("^w进入在线下单页面$")
    public void w进入在线下单页面() throws Throwable {
        WebElement onorder = Constant.driverWeb.findElement(By.linkText("在线下单"));
        onorder.click();
    }

    @Given("^w对这个订单进行星评价：(.+)$")
    public void w对这个订单进行数量星评价(String num) throws Throwable {
        System.out.println(Constant.OrderNumbuter.get(Constant.OrderNumbuter.size()-1));
        System.out.println(num);
        WebElement evaluate = Constant.driverWeb.findElement(By.xpath("//*[@id=\"star"+Constant.OrderNumbuter.get(Constant.OrderNumbuter.size()-1)+"\"]/img["+num+"]"));//点击评价星级
//            WebElement evaluate = Constant.driverWeb.findElement(By.xpath("//*[@id=\"starTDH2017082116199596\"]/img["+num+"]"));//点击评价星级
        evaluate.click();
        Thread.sleep(1000);

    }

    @Given("^w确认评价成功$")
    public void w确认评价成功() throws Throwable {
        WebElement evaluate = Constant.driverWeb.findElement(By.xpath("//*[@id=\"tipMsg\"]"));
        String result = evaluate.getText();
        Assert.assertEquals(result,"评价成功!");

    }

    @Given("^w选择因闪送员态度恶劣$")
    public void w选择因闪送员态度恶劣() throws Throwable {
        WebElement att = Constant.driverWeb.findElement(By.xpath("//*[@id=\"evaluation"+Constant.OrderNumbuter.get(Constant.OrderNumbuter.size()-1)+"\"]/div[3]"));//点击评价星级
        att.click();
    }

    @Given("^w提交差评$")
    public void w提交差评() throws Throwable {
        Constant.driverWeb.findElement(By.xpath("//*[@id=\"feedbackForm_"+Constant.OrderNumbuter.get(Constant.OrderNumbuter.size()-1)+"\"]/div[2]/button")).click();
    }

    @Given("^w订单成功被取消$")
    public void w订单成功被取消() throws Throwable {
        boolean btn = Method.qeuryAllOrderContent(Constant.OrderNumbuter.get(Constant.OrderNumbuter.size()-1),"已取消");
        Assert.assertEquals(btn,true);

    }

}
